<?php
$bartUserServices = new BartuserService();
$orderServices = new OrdersService();
$tool = new Tool();
if(isset($_GET['opt']))
{
	$invoiceService = new InvoiceService();
	$opt = $_GET['opt'];
	$moduleID = $moduleService->getModuleIDByFuncAndOpt($context, $result, $func, $opt);
	$permit = $permitService->checkPermition($context, $result, $_SESSION['user']['type'], $moduleID);
	if($opt == "add" && $permit ==true)
	{
		if(isset($_GET['id']))
		{
			$shipmentService = new ShipmentService();
			$order = $orderServices->getPurchaseOrderByPurchaseIDByFactoryID($context, $result, $_GET['id'], $_SESSION['user']['ID']);
			$orderDetailArr = $order->getOrderDetail();
			if(!Tool::isEmpty($orderDetailArr))
			{
				$smarty->assign('strCurency', $orderDetailArr[0]->getOrderdetailsDomain()->getCurrency());
			}
			$smarty->assign('order', $order);
			$shippedArr = array();
			foreach ($orderDetailArr as $orderDetail)
			{
				$shipped = $shipmentService->getShippedQuantity($context, $result, $_GET['id'], $orderDetail->getArticle()->getOid());
				$shippedArr[$orderDetail->getArticle()->getOid()] = $shipped;
			}
			$smarty->assign('shippedArr', $shippedArr);
			$smarty->display($templateDir . "/temp1/addInvoice.tpl");
		}
		
	}
	if($opt == "edit" && $permit ==true)
	{
		if(isset($_GET['id']) && is_numeric($_GET['id']))
		{
			$smarty->display($templateDir . "/temp1/editInvoice.tpl");
		}
	}
	if($opt == 'viewInvoice' && $permit ==true)
	{
		$smarty->assign('tool', $tool);
		if($_SESSION['user']['type'] == 3)
		{
			$invoiceArr = $invoiceService->getInvoiceDisplayByOrderID($context, $result, $_GET['id']);
			foreach ($invoiceArr as $invoice)
			{
				if($invoice->getInvoicesDomain()->getStatus() == 0)
				{
					$notJetConfirm = true;
					$smarty->assign('notJetConfirm', $notJetConfirm);
				}
			}
		}
		else if($_SESSION['user']['type'] == 2)
		{
			$invoiceArr = $invoiceService->getInvoiceDisplayByOrderIDAndFactID($context, $result, $_GET['id'], $_SESSION['user']['ID']);
		}
		else if($_SESSION['user']['type'] == 1)
		{
			$invoiceArr = $invoiceService->getInvoiceDisplayByOrderIDAndCusID($context, $result, $_GET['id'], $_SESSION['user']['ID']);
		}
		$smarty->assign('userType', $_SESSION['user']['type']);
		$smarty->assign('orderID', $_GET['id']);
		$smarty->assign('invoiceArr', $invoiceArr);
		$smarty->display($templateDir . "/temp1/listInvoiceDetail.tpl");
	}
	if($opt == 'viewInvoiceDetail' && $permit ==true)
	{
		$invoice = $invoiceService->getInvoiceByInvoiceID($context, $result, $_GET['id']);
		$smarty->assign('invoice', $invoice);
		if($_SESSION['user']['type'] == 2)
		{
			$isFact = true;
			$smarty->assign('isFact', $isFact);
		}
		$smarty->display($templateDir . "/temp1/viewInvoiceDetail.tpl");
	}
	if($opt == "list" && $permit ==true)
	{
		$start=$p->findStart($configPage['limit']);
		$count=$orderServices->countByStatus($context, $result, 4);
		$pages=$p->findPages($count,$configPage['limit']);
		$page_list = $p->pagesList($_GET['page'],$pages,$configPage);
		//$orderList = $orderServices->getAllInvoiceByRange($context, $result, $start, $configPage['limit']);
		$orderList = $orderServices->getAllOrderDisplayByTypeByRange($context, $result, 4, $start, $configPage['limit']);
		//print_r($orderList);
		
		$total = "Page ".$_GET['page']." in ".$pages. " Pages";
			
		$smarty->assign('total', $total);
		$smarty->assign('page_list', $page_list);
		$smarty->assign('orderList', $orderList);
		$smarty->display($templateDir . "/temp1/listInvoice.tpl");
	}
	if($opt == "invoiceListFact" && $permit ==true)
	{
		$start=$p->findStart($configPage['limit']);
		//$count=$orderServices->countByStatus($context, $result, 4);
		$count=$orderServices->countAllOrderbyFactoryByStatus($context, $result,$_SESSION['user']['ID'], "4");
		$pages=$p->findPages($count,$configPage['limit']);
		$page_list = $p->pagesList($_GET['page'],$pages,$configPage);
		//$orderList = $orderServices->getAllInvoiceByRange($context, $result, $start, $configPage['limit']);
		//$orderList = $orderServices->getAllOrderDisplayByTypeByRange($context, $result, 4, $start, $configPage['limit']);
		$orderList = $orderServices->getAllOrderByFactoryByStatus($context, $result, $start, $configPage['limit'], $_SESSION['user']['ID'], "4");
		//print_r($orderList);
		
		$total = "Page ".$_GET['page']." in ".$pages. " Pages";
			
		$smarty->assign('total', $total);
		$smarty->assign('page_list', $page_list);
		$smarty->assign('orderList', $orderList);
		$smarty->display($templateDir . "/temp1/listInvoice.tpl");
	}
	
	if($opt == "invoiceListCus" && $permit ==true)
	{
		$start=$p->findStart($configPage['limit']);
		//$count=$orderServices->countByStatus($context, $result, 4);
		$count=$orderServices->countAllOrderbyConsigneeByStatus($context, $result,$_SESSION['user']['ID'], "4");
		$pages=$p->findPages($count,$configPage['limit']);
		$page_list = $p->pagesList($_GET['page'],$pages,$configPage);
		//$orderList = $orderServices->getAllInvoiceByRange($context, $result, $start, $configPage['limit']);
		//$orderList = $orderServices->getAllOrderDisplayByTypeByRange($context, $result, 4, $start, $configPage['limit']);
		$orderList = $orderServices->getAllOrderByConsigneeByStatus($context, $result, $start, $configPage['limit'], $_SESSION['user']['ID'], "4");
		//print_r($orderList);
		
		$total = "Page ".$_GET['page']." in ".$pages. " Pages";
			
		$smarty->assign('total', $total);
		$smarty->assign('page_list', $page_list);
		$smarty->assign('orderList', $orderList);
		$smarty->display($templateDir . "/temp1/listInvoice.tpl");
	}
	
	if($opt == "searchInvoice" && $permit ==true)
	{
		if(isset($_POST['dateFrom']) && isset($_POST['dateTo']) && $_POST['dateFrom'] != '' && $_POST['dateTo'] != '')
		{
			$dateFrom = $_POST['dateFrom'];
			$dateTo = $_POST['dateTo'];
		}
		else {
			$dateFrom = '0000-00-00';
			$dateTo = date('Y-m-d', time());
		}

		$start=$p->findStart($configPage['limit']);
		if($_SESSION['user']['type'] == 3)
		{
			$count = $orderServices->countAllByStatusByOrderDate($context, $result, "4", $dateFrom, $dateTo);
			$pages=$p->findPages($count,$configPage['limit']);
			$page_list = $p->pagesList($_GET['page'],$pages,$configPage);
			$orderList = $orderServices->findAllByStatusByOrderDateByRange($context, $result, "4", $start, $configPage['limit'], $dateFrom, $dateTo);
		}
		else if($_SESSION['user']['type'] == 2)
		{
			//$count = $orderServices->countAllByStatusByOrderDate($context, $result, "4", $dateFrom, $dateTo);
			$count = $orderServices->countAllOrderbyFactoryByStatusOrderDate($context, $result, $_SESSION['user']['ID'], "4", $dateFrom, $dateTo);
			$pages=$p->findPages($count,$configPage['limit']);
			$page_list = $p->pagesList($_GET['page'],$pages,$configPage);
			//$orderList = $orderServices->findAllByStatusByOrderDateByRange($context, $result, "4", $start, $configPage['limit'], $dateFrom, $dateTo);
			$orderList = $orderServices->findAllByStatusByOrderDateByRangeByFactory($context, $result, "4", $start, $configPage['limit'], $dateFrom, $dateTo, $_SESSION['user']['ID']);
		}
		else if($_SESSION['user']['type'] == 1)
		{
			//$count = $orderServices->countAllByStatusByOrderDate($context, $result, "4", $dateFrom, $dateTo);
			$count = $orderServices->countAllByStatusByConsigneeOrderDate($context, $result, "4", $_SESSION['user']['ID'], $dateFrom, $dateTo);
			$pages=$p->findPages($count,$configPage['limit']);
			$page_list = $p->pagesList($_GET['page'],$pages,$configPage);
			//$orderList = $orderServices->findAllByStatusByOrderDateByRange($context, $result, "4", $start, $configPage['limit'], $dateFrom, $dateTo);
			$orderList = $orderServices->findAllByStatusByConsigneeOrderDateByRange($context, $result, "4", $start, $configPage['limit'],$_SESSION['user']['ID'], $dateFrom, $dateTo);
		}
		
		$total = "Page ".$_GET['page']." in ".$pages. " Pages";
			
		$smarty->assign('total', $total);
		$smarty->assign('page_list', $page_list);
		$smarty->assign('orderList', $orderList);
		$smarty->display($templateDir . "/temp1/listInvoice.tpl");
		
	}
}
?>